package edu.njfu.sas.servlet;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.sun.javaws.progress.PreloaderPostEventListener;
import edu.njfu.sas.dao.StudentDao;
import edu.njfu.sas.dao.impl.StudentDaoImpl3;
import edu.njfu.sas.model.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

@WebServlet("/ListStudentByPage" )
public class ListStudentByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        int curPage=Integer.parseInt(request.getParameter("curPage"));
        int pageSize=Integer.parseInt(request.getParameter("pageSize"));
        StudentDaoImpl3 studentDaoImpl3 = new StudentDaoImpl3();
        List<Student> studentList=studentDaoImpl3.getStudentByPaging(curPage,pageSize);
        long count=studentDaoImpl3.totalRecords();
        HashMap<String,Object>map=new HashMap<>();
        map.put("Maxpage",count/pageSize+1);
        map.put("students",studentList);
        PrintWriter out= response.getWriter();
        out .write(new Gson().toJson(map));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
